// TOWN SCRIPT

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

begintownscript;

variables;

int i;
int counter = 0;
int mayordeath;
int sound,toplay;

body;

beginstate INIT_STATE;
// Called upon entering

i = (get_flag(250,1));

if(get_flag(250,0) == 1) { //if good
	//Set up characters,
	activate_hidden_group(1);
	activate_hidden_group(8); // townspeople friendly
	
	//Set up stains/terrains
	//SE
	set_terrain(50,33,477);
	set_terrain(52,32,482);
	set_terrain(51,31,481);
	put_stain_on_space(51,33,2);
	put_stain_on_space(50,31,0);
	put_stain_on_space(53,32,1);
	//NW
	set_terrain(11,27,478);
	set_terrain(12,25,479);
	set_terrain(9,23,482);
	put_stain_on_space(11,26,0);
	put_stain_on_space(9,24,0);
	put_stain_on_space(9,27,1);	
	//SW
	set_terrain(14,56,477);
	set_terrain(13,52,478);
	set_terrain(15,53,481);
	put_stain_on_space(13,55,1);
	put_stain_on_space(14,53,0);
	put_stain_on_space(12,53,0);	
	//bakery
	put_stain_on_space(27,48,4);
	set_terrain(27,48,481);
}


if(get_flag(250,0) == 2) { //if bad
	relocate_character(i,58,43);
	force_view_center(58,43);
	force_instant_terrain_redraw();
	activate_hidden_group(3);
	activate_hidden_group(9);
}	
break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;
// Called every turn.
if(get_flag(1,1) == 1) {
	end();
} else {
	if((sound) < 11) {
		sound = sound + 1;
	if((sound) == 3) {
		toplay = (get_ran(1,1,8));
	
		if((toplay) == 1) {
			play_sound(-5);
		}
		
		if((toplay) == 2) {
			play_sound(-21);
		}
	
		if((toplay) == 3) {
			play_sound(-19);
		}
	
		if((toplay) == 4) {
			play_sound(-29);
		}
		
		if((toplay) == 5) {
			play_sound(-51);
		}
		
		if((toplay) == 6) {
			play_sound(-118);
		}
	
		if((toplay) == 7) {
			play_sound(-152);
		}
	
		if((toplay) == 8) {
			play_sound(-164);
		}
		sound = 0;
		}
	}
}

if(get_flag(250,0) == 1) { //If good

	//Good guy climax - have to run to mayor
	if((get_flag(0,14) == 1) && (get_flag(0,17) == 0)) {
		if((counter) != 3) {
			counter = counter + 1;
		} else {
			counter = counter + 1; //never happens again
			reset_dialog();
			add_dialog_str(0,"Suddenly, you see Dervish Stieger running towards you, hotly followed by several of your comrades. She looks furious...",0);
			add_dialog_str(1,"_It's good to see you, soldier. These bastards are far better trained than we'd thought. Casualties are high, but at least now you've completed your orders, they shouldn't climb much more. Which leads me to our new problem...",0);
			add_dialog_str(2,"_They've managed to get past all our patrols, and are heading for Town Hall - and Mayor Rourke. We've got to get up there to defend him. Come on - we don't have much time!_",0);
			add_dialog_str(3,"She flies off towards the northwest...",0);
			run_dialog(1);
			flip_terrain(21,13);
			flip_terrain(21,14);
			//Change bookshelves
			set_terrain(18,11,403);
			set_terrain(13,11,403);
			//Destroy floor
			set_terrain(16,14,137);
			set_terrain(16,12,137);
			set_terrain(17,13,137);
			set_terrain(18,15,137);
			set_terrain(14,12,137);
			set_terrain(15,12,480);
			set_terrain(14,14,479);
			//Items
			put_item_on_spot(18,11,185);
			put_item_on_spot(19,11,185);
			put_item_on_spot(17,11,185);
			put_item_on_spot(14,11,185);
			put_item_on_spot(13,11,185);
			put_item_on_spot(12,11,185);
			//Refresh this
			force_instant_terrain_redraw();
			toggle_quest(1,0);
			toggle_quest(2,1);
			set_flag(0,17,1);
			//Set timer depending on loc
			if(get_flag(0,11) == 2) {
				mayordeath = 50;
			} 
			if(get_flag(0,12) == 2) {
				mayordeath = 40;
			} 
			if(get_flag(0,13) == 2) {
				mayordeath = 25;
			}
		}
	}

	if((get_flag(0,17) == 1) && (get_flag(0,16) == 0)) {
		mayordeath = mayordeath - 1;
	}


	if((get_flag(0,17) == 1) && (mayordeath) == 0) {
		message_dialog("You hear a scream from the north-west corner of town, and start to run towards it. As you get closer, you see people streaming out of Town Hall and into the streets.","Suddenly, a huge explosion shatters the earth, sending chunks of masonry flying into the sky. You've failed - Mayor Rourke is dead, and Town Hall lies in rubble. You flee to the nearest escape tunnel and flee.");
		end_scenario(1);
	}

	if(get_flag(0,18) == 6 && get_flag(1,1) == 0) {
		message_dialog("The last of the invaders falls, and you stare at the bloody floor, on which the dead bandits lie, together with some of your fallen comrades. You hear some screams outside, though, and you turn around.","From outside you can hear people shouting. _They're not all dead! They're fleeing!_ You should quickly chase all of the remaining invaders to the blasted part of the walls!");
		activate_hidden_group(5);
		set_flag(1,1,1);
		}

}

if(get_flag(250,0) == 2) { //EVIL
	if(get_flag(250,2) == 5) {
		print_str_color("You feel stronger!",3);
		change_pc_skill_pts(i,2);
		set_flag(250,2,0);
	}

	//bad guy climax, run to town hall
	if((get_flag(0,10) == 3) && (get_flag(0,17) == 0)) {
		if((counter) != 3) {
			counter = counter + 1;
		} else {
			counter = counter + 1; //never happens again
			reset_dialog();
			add_dialog_str(0,"Suddenly, you see Steven running towards you, followed by several of your comrades. He slows to a halt and grins.",0);
			add_dialog_str(1,"_We're done, and I can see that you are too... good._ He pauses to catch his breath. _We need to head over to Town Hall straight away and deal with the Mayor. I've found out it's to the north-west.",0);
			add_dialog_str(2,"_And we need to be quick - there's bound to be some Imperial bastards on our trail - I actually heard the Captain here swear as she found the smashed gates, so she can't be that far behind us. Come on!_",0);
			add_dialog_str(3,"He flies off to the northwest...",0);
			run_dialog(1);
			set_flag(0,17,1);
			//Set timer depending on loc
			if(get_flag(0,11) == 2) {
				mayordeath = 50;
			} 
			if(get_flag(0,12) == 2) {
				mayordeath = 40;
			} 
			if(get_flag(0,13) == 2) {
				mayordeath = 25;
			}
			//Adjust town hall so that it looks like its being attacked
			//open doors
			flip_terrain(21,13);
			flip_terrain(21,14);
			//Change bookshelves
			set_terrain(18,11,403);
			set_terrain(13,11,403);
			//Destroy floor
			set_terrain(16,14,137);
			set_terrain(16,12,137);
			set_terrain(17,13,137);
			set_terrain(18,15,137);
			set_terrain(14,12,137);
			//Items
			put_item_on_spot(18,11,185);
			put_item_on_spot(19,11,185);
			put_item_on_spot(17,11,185);
			put_item_on_spot(14,11,185);
			put_item_on_spot(13,11,185);
			put_item_on_spot(12,11,185);
			//Refresh this
			force_instant_terrain_redraw();
			toggle_quest(3,0);
			toggle_quest(4,1);
			}
		}

	if((get_flag(0,17) == 1) && (get_flag(0,16) == 0)) {
		mayordeath = (mayordeath - 1);
	}

	if((get_flag(0,17) == 1) && (mayordeath) == 0 && get_flag(1,3) == 0) {
		message_dialog("You hear a scream from the north-west corner of town. Suddenly, a swarm of Imperial soldiers stream out on to the streets from that direction.","You stop dead, turn and begin running - you were too slow and Mayor Rourke survived the attack. Now you need to get away before the soldiers find you! You should get back to the blasted wall.");
		activate_hidden_group(6);
		erase_char(28);
		erase_char(47);
		erase_char(48);
		set_flag(1,3,1);
	}
	
	if(get_flag(0,18) == 6 && get_flag(1,3) == 0) {
		message_dialog("You pause to catch your breath after dispatching the last of the mayor's bodyguards. Finally, you have succeeded. Your joy is shortlived however - you hear the shouting of Empire troops marching this way...","Any plan you had of holding this town are moot - all that matters now is staying alive - the Empire will double its efforts to bring you to justice. Enough reason to leave this place: get back to the blasted walls...");
		activate_hidden_group(6);
		erase_char(28);
		erase_char(47);
		erase_char(48);
		set_flag(1,3,1);
		set_flag(1,5,1);
	}
}
break;

//Gravestones
beginstate 10;
	if(get_flag(250,0) == 1) {
		if(get_flag(0,1) == 0) {
			message_dialog("This tombstone, like all the others in the cemetery, bears the name of a citizen of New Haven. Soldiers and other important people, such as the city's Mayor, all have their own private cemetery outside of town.","There's nothing you can really do here - the living citizens of town need your attention more than the dead do right now.");
			set_flag(0,1,1);
			end();
		} else {
			text_bubble_on_char(i,"There's nothing I can do here.");
			force_instant_terrain_redraw();
			block_entry(1);
		}
	} else {
		if(get_flag(0,1) == 0) {
			message_dialog("This tombstone, like all the others in the cemetery, bears the name a citizen of New Haven. Glancing around, you see that there are no soldiers or leaders here - they must be buried elsewhere.","Still, these people are guilty of association with the Empire, and they deserve to die for that crime.");
			set_flag(0,1,1);
			end();
		} else {
			text_bubble_on_char(i,"There's nothing I can do here.");
			force_instant_terrain_redraw();
			block_entry(1);
		}
	}
break;

//Temple bookshelves
beginstate 11;
	if(get_flag(250,0) == 1) { //If good
		if(get_flag(0,2) == 0) {
			message_dialog("These shelves contain the private journals and books of Father Simmons. Even if you had the time to read them, you wouldn't - everybody is entitled to their privacy.","You leave the shelves and their contents alone.");
			set_flag(0,2,1);
			end();
		} else {
			text_bubble_on_char(i,"I've already seen this.");
			force_instant_terrain_redraw();
		}
	} else { //if bad
		if(get_flag(0,2) == 0) {
			message_dialog("These shelves contain the private journals and books of the town's priest. You look them over, and pull a few out. As you reach for a novel on the top shelf, however, you spot that the books there are actually fake.","You pull them away and find a stash of gold. Clearly the priest didn't want anybody in town to find it - presumably it was ill-gotten. Too bad he couldn't hide it from you...");
			change_coins(200);
			set_flag(0,2,1);
			end();
		} else {
			text_bubble_on_char(i,"I've already seen this.");
			force_instant_terrain_redraw();
		}
	}
break;
		
//Entering priests room
beginstate 12;
	if(get_flag(250,0) == 1) { //If good
		if(get_flag(0,3) == 0) {
			message_dialog("You step through the curtain and into the private room of Father Simmons. It's a fairly simple place - there are two bookcases full of literature, and an open tome set off to the side.","Apart from these details, though, there's nothing of interest in here.");
			set_flag(0,3,1);
		}
	} else { //if bad
		if(get_flag(0,3) == 0) {
			message_dialog("You step through the curtain and into the private room of the town's priest. It all looks fairly simple - there are two bookcases full of literature, an open tome set off to the side, and a bed placed against the far wall.","What catches your eye though, is the tome - the writing on it looks fresh. It would only take a moment to read it...");
			set_flag(0,3,1);
		}
	}
break;

//Book in priest's room
beginstate 13;
	if(get_flag(250,0) == 1) { //If good
		if(get_flag(0,4) == 0) {
			reset_dialog();
			add_dialog_str(0,"You've seen this book many times - it's the tome Father Simmons uses to treat injuries and diseases in town. Hopefully there will be some enchantment inside that will be able to heal you...",0);
			add_dialog_str(1,"Sure enough, you flick through the tome and find a simple restorative spell. However, there's a warning at the bottom - it can only be used once per day. Do you want to use it now?",0);
			add_dialog_choice(0,"Yes, use the spell.");
			add_dialog_choice(1,"No, wait before using it.");
			if(run_dialog(1) == 1) {
				set_flag(0,4,1);
				revive_party();
				print_str_color("You are restored.",2);
			} else {
				end();
			}
		} else {
			text_bubble_on_char(i,"I've already read this.");
			force_instant_terrain_redraw();
			end();
		}
	} else { //if bad
		if(get_flag(0,4) == 0) {
			reset_dialog();
			add_dialog_str(0,"This tome looks like some kind of encyclopaedia of medical spells. Whilst many of them are too complicated or too bizarre to be of any use to you, you should be able to find something inside that will heal you...",0);
			add_dialog_str(1,"Sure enough, you flick through the tome and find a simple restorative spell. However, there's a warning at the bottom - it can only be used once per day. Do you want to use it now?",0);
			add_dialog_choice(0,"Yes, use the spell.");
			add_dialog_choice(1,"No, wait before using it.");
			if(run_dialog(1) == 1) {
				set_flag(0,4,1);
				revive_party();
				print_str_color("You are restored.",2);
			} else {
				end();
			}
		} else {
			text_bubble_on_char(i,"I've already read this.");
			force_instant_terrain_redraw();
			end();
		}
	}
break;

//Temples box
beginstate 14;
	if(get_flag(250,0) == 1) { //If good
		if(get_flag(0,5) == 0) {
			message_dialog("This chest contains the donations people are expected to give to the church. You have no business depositing any coins - as a soldier you are exempt from donating, and you have no right to take any of the gold already here.","You leave the chest alone.");
			block_entry(1);		
			set_flag(0,5,1);
		} else {
			text_bubble_on_char(i,"I can't do anything here.");
			force_instant_terrain_redraw();
			block_entry(1);
		}
	} else { //if bad
		if(get_flag(0,5) == 0) {
			reset_dialog();
			add_dialog_str(0,"This chest contains a small amount of coins - you assume that they are donations given to the church by the citizens of New Haven.",0);
			add_dialog_str(1,"Despite there only being a meagre amount of coins in the bottom of the chest, you could still relieve the church of them. Do you?",0);
			add_dialog_choice(0,"Yes, take the coins.");
			add_dialog_choice(1,"No, leave them alone.");
			if(run_dialog(1) == 1) {
				set_flag(0,5,1);
				change_coins(24);
			} else {
				end();
			}
		} else {
			text_bubble_on_char(i,"I can't do anything else here.");
			force_instant_terrain_redraw();
			end();
		}
	}
break;

//Gates doors both good and bad
beginstate 15; 
	text_bubble_on_char(i,"There is no need for me to go there.");
	force_instant_terrain_redraw();
	block_entry(1);
break;

//Temple greeting
beginstate 16;
	if(get_flag(250,0) == 1) { //If good
		if(get_flag(0,6) == 0) {
			message_dialog("You enter New Haven's temple. The place is deserted - everybody must have fled here already. It's eerie how quiet this place is, despite the carnage happening outside.","Since there is nobody inside, you probably shouldn't linger for too long - the longer you stay here, the more likely it is that somebody outside dies.");
			set_flag(0,6,1);
		}
	} else { //if bad
		if(get_flag(0,6) == 0) {
			message_dialog("You enter New Haven's temple. The place is deserted - everybody must have fled here already - and you can take a moment to look around.","Everything here looks rather mundane, from the crude stone altar to the bare stone walls. You can't imagine it inspires the people here in any way...");
			set_flag(0,6,1);
		}
	}
break;

beginstate 17;
break;

//Leaving town, both good and bad
beginstate 18;
if (get_flag(1,4) == 1 && get_flag(250,0) == 1) { // if good and task completed
		message_dialog("You have already chased the invaders away. It would be of no use to try and chase them outside of town.","");
		block_entry(1);
		}
if (get_flag(1,4) == 1 && get_flag(250,0) == 2 && get_flag(1,5) == 1) { // if bad and task completed
		message_dialog("You quickly run out of town. When you are at a safe distance, you stop and turn around. You look at New Haven. It was a beautiful city, but it had a rotten mayor.","You then silently walk on, into the woods surrounding New Haven, pleased with yourself, and the fortunate outcome of this attack. Your lives will be somewhat more difficult, but your parents are avenged. THE END");
		end_scenario(1);
		}
if (get_flag(1,4) == 1 && get_flag(250,0) == 2 && get_flag(1,5) == 0) { // if bad and task failed
		message_dialog("Chased by the guards, you and your remaining friends retreat from town. You didn't kill the mayor and you failed your parents. The only result of this attack, was that the Empire gave your head a price tag.","THE END");
		end_scenario(0);
		}
	// otherwise:
	message_dialog("This way leads away from New Haven, and away from your task. Whilst it is tempting to just rush off back to the relative safety of the wilderness, you have a task to do - the Empire must learn of your fury.","You turn back.");
	block_entry(1);
break;

//Commander's Shelves
beginstate 19;
	if(get_flag(250,0) == 1) { //If good
		if(get_flag(0,7) == 0) {
			message_dialog("These shelves contain various scrolls and sheets, each one filled with troop movements and guard rosters. Everything is in Imperial shorthand - rendering it meaningless to non-military personnel.","Needless to say, it is as boring as any Empire document you've ever seen - you learn nothing useful from it at all.");
			set_flag(0,7,1);
			end();
		} else {
			text_bubble_on_char(i,"I've already seen this.");
			force_instant_terrain_redraw();
		}
	} else { //if bad
		if(get_flag(0,7) == 0) {
			message_dialog("These shelves contain various scrolls and sheets, each one filled with squiggles and gibberish. All you can read are the titles of each document, and even those are meaningless to you.","Needless to say, you soon get frustrated with the documents and put them back. You have bigger fish to fry...");
			set_flag(0,7,1);
			end();
		} else {
			text_bubble_on_char(i,"I've already seen this.");
			force_instant_terrain_redraw();
		}
	}
break;

//Barracks door
beginstate 20;
	if(get_flag(250,0) == 1) { //If good
		if(get_flag(0,9) == 0) {
			message_dialog("You should speak to Dervish Stieger before you leave the barracks - it's likely she has orders for you.","");
			block_entry(1);
		}
	} else { //if bad
		if(get_flag(0,25) == 0) {
			message_dialog("You enter the New Haven barracks with a hint of trepidation - whilst most, if not all, of the soldiers stationed here will be out fighting, you're still entering their base, and probably the most secure place in New Haven.","Fortunately, there aren't any soldiers here now, although that could change in an instant.");
			set_flag(0,25,1);
		}
	}

if(get_flag(1,2) == 1) { // if good and task completed NO LONGER NEEDED, SORRY THRAL
		message_dialog("You return to your bed, tired of a morning full of activity. Whilst there was never any doubt that the Empire would win, it was a still tough fight, and they streets are full of the dead.","You, at least, survived, and by doing so you saved more lives. You should be proud of yourself... not everybody is a winner. THE END.");
		end_scenario(1);
	}
break;

//SE stairs
beginstate 21;
	if(get_flag(250,0) == 1) { //Goodie
		if(get_flag(0,10) != 2) { //if only been to one door
			if(get_flag(0,12) == 0) {
				set_flag(0,12,1); //Been here, can't open.
				message_dialog("This door has somehow been fused into the wall that surrounds it. There's no way you'll be able to get it open...","");
				inc_flag(0,10,1);
				block_entry(1);
				end();
			} else {
				text_bubble_on_char(i,"I can't get in here...");
				force_instant_terrain_redraw();
				block_entry(1);
				end();
			}
		} else {
			if(get_flag(0,12) == 1) {
				text_bubble_on_char(i,"I can't get in here...");
				force_instant_terrain_redraw();
				block_entry(1);
				end();
			} else {
				if(get_flag(0,12) == 0) {
					message_dialog("You rush over to this door - this one, unlike the others you have found, has not been blocked off yet...","You open the door. It'd be best to leave it open, so people can flee this town quickly.");
					set_flag(0,12,2); //Been here, can open.
					flip_terrain(51,34);// open door
					set_flag(0,14,1);
				}
			}
		}
	}
	
	if(get_flag(250,0) == 2) { //if bad
		if(get_flag(0,20) == 1) {
			text_bubble_on_char(i,"I've just sealed this door...");
				force_instant_terrain_redraw();
				block_entry(1);
				end();
			}
	}
break;

//SW stairs
beginstate 22;
	if(get_flag(250,0) == 1) { //Goodie
		if(get_flag(0,10) != 2) {
			if(get_flag(0,11) == 0) {
				set_flag(0,11,1); //Been here, can't open.
				message_dialog("This door has somehow been fused into the wall that surrounds it. There's no way you'll be able to get it open...","");
				inc_flag(0,10,1);
				block_entry(1);
				end();
			} else {
				text_bubble_on_char(i,"I can't get in here...");
				force_instant_terrain_redraw();
				block_entry(1);
				end();
			}
		} else {
			if(get_flag(0,11) == 1) {
				text_bubble_on_char(i,"I can't get in here...");
				force_instant_terrain_redraw();
				block_entry(1);
				end();
			} else {
				if(get_flag(0,11) == 0) {
					message_dialog("You rush over to this door - this one, unlike the others you have found, has not been blocked off yet...","You open the door. It'd be best to leave it open, so people can flee this town quickly.");
					set_flag(0,11,2); //Been here, can open.
					flip_terrain(12,54); // open door
					set_flag(0,14,1);
				}
			}
		}
	}
	if(get_flag(250,0) == 2) { //if bad
		if(get_flag(0,19) == 1) {
			text_bubble_on_char(i,"I've just sealed this door...");
				force_instant_terrain_redraw();
				block_entry(1);
				end();
			}
	}
break;

//NW stairs
beginstate 23;
	if(get_flag(250,0) == 1) { //Goodie
		if(get_flag(0,10) != 2) {
			if(get_flag(0,13) == 0) {
				set_flag(0,13,1); //Been here, can't open.
				message_dialog("This door has somehow been fused into the wall that surrounds it. There's no way you'll be able to get it open...","");
				inc_flag(0,10,1);
				block_entry(1);
				end();
			} else {
				text_bubble_on_char(i,"I can't get in here...");
				force_instant_terrain_redraw();
				block_entry(1);
				end();
			}
		} else {
			if(get_flag(0,13) == 1) {
				text_bubble_on_char(i,"I can't get in here...");
				force_instant_terrain_redraw();
				block_entry(1);
				end();
			} else {
				if(get_flag(0,13) == 0) {
					message_dialog("You rush over to this door - this one, unlike the others you have found, has not been blocked off yet...","You open the door. It'd be best to leave it open, so people can flee this town quickly.");
					set_flag(0,13,2); //Been here, can open.
					flip_terrain(10,24); // open door
					set_flag(0,14,1);
				}
			}
		}
	}
	if(get_flag(250,0) == 2) { //if bad
		if(get_flag(0,21) == 1) {
			text_bubble_on_char(i,"I've just sealed this door...");
				force_instant_terrain_redraw();
				block_entry(1);
				end();
			}
	}
break;

//Stairwells
beginstate 24;
	if(get_flag(250,0) == 1) { //If good
		if(get_flag(0,15) == 0) {
			message_dialog("These stairs lead down into the tunnels beneath the city, eventually leading out into the mountains that surround New Haven. Right now your mission is to make sure this path is clear, not to escape down it. You turn back.","");
			set_flag(0,15,1);
			block_entry(1);
		}
	} 
break;

//Got to mayors door
beginstate 25;
	if(get_flag(250,0) == 1) { //Goodie
		if((get_flag(0,16) == 0) && (get_flag(0,17) == 1)) {
			message_dialog("You reach the doors to Town Hall, which have been thrown wide open. Inside, you can see Dervish Stieger in a pitched battle with several of the robed invaders. Two Empire soldiers lie dead on the ground beside her.","The invaders stand between you and your allies - you'll have to fight through them to help Stieger. Combat is joined.");
			activate_hidden_group(2);
			set_flag(0,16,1);
		}
		if(get_flag(0,17) == 0) {
			message_dialog("You have no reason to go into Town Hall right now.","");
			block_entry(1);
		}
		
	}
	
	if(get_flag(250,0) == 2) { //Baddie
		if((get_flag(0,16) == 0) && (get_flag(0,17) == 1)) {
			message_dialog("You reach the doors to Town Hall, which have been thrown wide open. Inside, you can see Steven and your allies in a pitched battle with several Imperial soldiers.","Steven half-turns and parries a blow from a soldier, calling out to you to help as he does. Combat is joined...");
			activate_hidden_group(4);
			set_flag(0,16,1);
		}
		if(get_flag(0,17) == 0) {
			message_dialog("You have no reason to go into Town Hall right now.","");
			block_entry(1);
		}
	}
break;

//Library shelves
beginstate 26;
	if(get_flag(250,0) == 1) { //If good
		if(get_flag(0,22) == 0) {
			message_dialog("These shelves contain the most popular books in the library - those that every good, literate, citizen of the Empire is expected to have read.","Browsing through the titles, you soon notice that there's nothing here you haven't read several times already. Needless to say, now isn't the time to start reading them again... You move on.");
			set_flag(0,22,1);
			end();
		} else {
			text_bubble_on_char(i,"I don't have time to read these.");
			force_instant_terrain_redraw();
		}
	} else { //if bad
		if(get_flag(0,22) == 0) {
			message_dialog("These shelves don't contain anything remotely interesting - most of the books here are typical Empire-authorised trash.","Needless to say you don't spend any time reading the tomes - even if they did appeal to you, now isn't the time to do so... You move on.");
			set_flag(0,22,1);
			end();
		} else {
			text_bubble_on_char(i,"There's nothing here worth reading.");
			force_instant_terrain_redraw();
		}
	}
break;

//Library greeting
beginstate 27;
	if(get_flag(250,0) == 1) { //If good
		if(get_flag(0,23) == 0) {
			message_dialog("You enter the New Haven Library. Fortunately, the place looks empty, and the floor is free of bodies and blood - the invaders must not have made it here yet.","Apart from the place being deserted, everything else seems as it should.");
			set_flag(0,23,1);
		}
	} else { //if bad
		if(get_flag(0,23) == 0) {
			message_dialog("As you enter this building, you are greeted by the quasi-romantic smell of yellowing paper. Clearly, there are a lot of books inside, although all you can see are a few bookcases.","There must be more books further inside...");
			set_flag(0,23,1);
		}
	}

break;

//Library steps
beginstate 28;
	if(get_flag(250,0) == 1) { //If good
		if(get_flag(0,24) == 0) {
			reset_dialog();
			add_dialog_str(0,"Beneath this trapdoor are a series of steps leading down into the library proper. You unbolt it, open it and peer down, noticing that something isn't right - the air down there smells strongly of smoke and blood...",0);
			add_dialog_str(1,"You take a few steps down, and freeze - the place is aflame, and has been for at least ten minutes.  At the bottom of the steps is the librarian, who evidently tried to hide here when the attackers came.",0);
			add_dialog_str(2,"Whilst you can't be sure of what exactly happened to her, you have a fairly good idea - most likely one of the attackers found her hiding down there and set the place alight, trapping and killing her.",0);
			add_dialog_str(3,"The cold-bloodedness of this act makes your gorge rise, and you grip your sword with fierce determination. These bastards must pay for daring to attack the Empire and her people...",0);
			run_dialog(1);
			set_flag(0,24,1);
			block_entry(1);
			end();
		} else {
			text_bubble_on_char(i,"There's nothing else I can do here.");
			force_instant_terrain_redraw();
			block_entry(1);
		}	
	} else { //if bad
		if(get_flag(0,24) == 0) {
			reset_dialog();
			add_dialog_str(0,"Beneath this trapdoor are a series of steps leading down into the library proper - this must be where the rest of the books are kept. As you start down the steps, you hear a whimper coming from the shadows; somebody is down here.",0);
			add_dialog_str(1,"Suddenly, a thought crosses your mind - if you were to toss a bolt of fire into this room, it would almost certainly strike a bookcase and cause a fire, killing everybody hiding down here.",0);
			add_dialog_str(2,"Do you want to do that?",0);
			add_dialog_choice(0,"Yes, kill the bastards.");
			add_dialog_choice(1,"No, let them live.");
			if(run_dialog(1) == 1) { //FRY FRY FRY! (p.s, meat is murder)
				reset_dialog();
				add_dialog_str(0,"Lazily, you summon a ball of flame and throw it into the room. It hits a bookcase, lighting the papers upon it. Suddenly, you see somebody run out from behind another shelf, heading for you.",0);
				add_dialog_str(1,"You step back, close the trapdoor, and bolt it, just as a young woman reaches the top of the stairs. After a moment or two, the furious hammering and scratching coming from the other side of the trapdoor dies away.",0);
				add_dialog_str(2,"You wait for a moment, savouring the blood you just spilt, before moving on.",0);
				run_dialog(1);
				set_flag(0,24,1);
				inc_flag(250,2,1);
				block_entry(1);
				end();
			} else {
				message_dialog("You walk away - there'll be time to see to whoever is down there later. In the meantime, you place some heavy tomes on the trapdoor, preventing anybody down there getting out.","");
				end();
			}
		} else {
			text_bubble_on_char(i,"There's nothing else I can do here.");
			force_instant_terrain_redraw();
			block_entry(1);
		}
	}
break;

//Crater 
beginstate 29;
if (get_flag(1,1) != 0)
		end();

	if(get_flag(250,0) == 1) { //If good
		if(get_flag(0,25) == 0) {
			message_dialog("You approach this huge crater slowly, anger boiling inside of you. This must be where the invaders smashed their way into New Haven.","The bodies of citizens and soldiers alike lie everywhere, and smoke still rises from the ground - whoever did this must be made to pay...");
			message_dialog("In the distance, you see several of the robed invaders. Despite your fury, it would be wise not to take these people on - you have a job to do, and you need to be alive to do it...","");
			block_entry(1);
			set_flag(0,25,1);
		} else {
			text_bubble_on_char(i,"I should stay away from there...");
			force_instant_terrain_redraw();
			block_entry(1);
		}
	} else { //if bad
		if(get_flag(0,9) == 0) {
			message_dialog("You should speak to Steven before you head off into town - it is likely he'll have a task for you to do.","");
			block_entry(1);
		}
	}
break;

//Barracks Lockers - stops baddies getting in
beginstate 30;
	if(get_flag(250,0) == 1) { //If good
		end();
	} else { //if bad
		message_dialog("These lockers are padlocked shut, and locked with a numerical code. Unfortunately, you don't have the time to try and guess it.","You move on.");
			block_entry(1);
	}
break;

//Bakery entrance
beginstate 31;
	if(get_flag(250,0) == 1) { //If good
		if(get_flag(0,26) == 0) {
			message_dialog("You enter New Haven's bakery and are greeted by the smell of burning. At first you think it's because the baker has left one of the ovens on.","However, a brief glance around shows you the real cause - behind the counters in the kitchen there is a charred and blackened body - the invaders must have been here before you.");
			set_flag(0,26,1);
		}
	} else { //if bad
		if(get_flag(0,26) == 0) {
			message_dialog("As you enter this small shop, you hear a muffled cry coming from your right - there is a youngish-looking woman hiding behind the counters there. As you walk inside, she draws her legs up to her chin, and begins sobbing...","Clearly, she's petrified of you...");
			set_flag(0,26,1);
		}
	}
break;

//Trainers shelves
beginstate 32;
	if(get_flag(0,28) == 0) {
		message_dialog("This shelf contains various books about fighting and adventuring. There are books on practically every subject imaginable...","Since the width of knowledge here is so large, the depth is pretty small - there isn't anything here you didn't already know. You move on.");			
		set_flag(0,28,1);
		
		end();
	} else {
		text_bubble_on_char(i,"I've already seen this.");
		force_instant_terrain_redraw();
	}
break;

//Archers shelves
beginstate 33;
	if(get_flag(0,27) == 0) {
		message_dialog("This shelf contains various books on fletchery, including some detailing how to make arrows from everyday material, some explaining how to shoot whilst blindfolded, and even some on the best way to care for a tree before it is carved into a bow.","Unfortunately, despite all of this, there isn't anything that would be even remotely useful to you... You move on.");			
		set_flag(0,27,1);
		end();
	} else {
		text_bubble_on_char(i,"I've already seen this.");
		force_instant_terrain_redraw();
	}
break;

//Gate wheels
beginstate 34;
	if(get_flag(250,0) == 1) { //If good
		if(get_flag(0,29) == 0) {
			message_dialog("As you get closer to this wheel, you see that it has been sabotaged: the chain that runs through the wheel, and down into the ground has been broken.","Until it has been completely replaced, there will be no way to get the gates that lead out of town open... This makes it all the more important for you to open the escape tunnels...");
			set_flag(0,29,1);
			end();
		} else {
			text_bubble_on_char(i,"I can't use this.");
			force_instant_terrain_redraw();
		}
	} else { //if bad
		if(get_flag(0,29) == 0) {
			message_dialog("As you get closer to this wheel, you see that it has been sabotaged: the chain that runs through the wheel, and down into the ground has been broken.","Until it has been repaired, there will be no way to get the gates that lead out of town open - and once you've completed your task, there will be no way out at all...");
			set_flag(0,29,1);
			end();
		} else {
			text_bubble_on_char(i,"I can't use this.");
			force_instant_terrain_redraw();
		}
	}
break;

//Lockers in storeroom
beginstate 35;
	text_bubble_on_char(i,"This locker is empty.");
	force_instant_terrain_redraw();
break;

//Storeroom
beginstate 36;
	if(get_flag(250,0) == 1) { //If good
		if(get_flag(1,0) == 0) {
			message_dialog("This storeroom is almost completely empty - there are just a few bits and pieces left on the tables. As soon as it was clear that New Haven was being attacked, this place would have been cleaned out and the gear inside given to soldiers.","Unfortunately, that means there will be very little here that you'll be able to take and use.");
			set_flag(1,0,1);
		}
	} else { //if bad
		if(get_flag(1,0) == 0) {
			message_dialog("This storeroom has been all but emptied - there are just a few bits and pieces left on the tables. Presumably the gear from here was equipped by the Imperial troops as soon as you attacked New Haven.","Unfortunately, that means there will be very little here that you'll be able to take and use.");
			set_flag(1,0,1);
		}
	}
break;

//Lockers in smithy
beginstate 37;
	text_bubble_on_char(i,"This locker is padlocked shut.");
	force_instant_terrain_redraw();
break;

// This happens once you arrive at blasted gates and chase away invaders
beginstate 38;
	if(get_flag(1,1) == 1 && get_flag(1,2) == 0) {
		message_dialog("You arrive at the walls, and watch as the last of the invaders run away. It seems that your fellow soldiers chased them away already.","One of them looks at you, relief, and grief, in his eyes. _You did good, kid. Now it's time to clean up...");
		message_dialog("In the hours that follow, the dead are lifted from the streets; their blood washed away from the cobbles. But, for now, there is no time for mourning - vengence must come first.","THE END");
		end_scenario(1);
		
		}
break;

beginstate 39;
	if(get_flag(1,3) == 1 && get_flag(1,4) == 0) {
		message_dialog("You arrive at the walls. It seems the town guards were expecting you to come this way. Some of them have been waiting for your retreat...","");
		activate_hidden_group(7);
		set_flag(1,4,1);
		}
break;